<template>
  <el-form ref="wyform" :model="form" :rules="rules" label-width="100px">
    <el-form-item label="标题" prop="title">
      <el-input v-model="form.title" placeholder="请输入标题"></el-input>
    </el-form-item>
    <el-form-item label="意见区" prop="text">
      <VueEditor
        useCustomImageHandler
        v-model="form.text"
        @image-added="handleImageAdded"
      ></VueEditor>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success" size="small" plain @click="save"
        >提交</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script>
import { VueEditor } from "vue2-editor";
export default {
  name: "adviceEdit",
  props: ["username"],
  components: {
    VueEditor,
  },
  data() {
    return {
      form: {
        title:'',
        username: "",
        text: "",
        email:''
      },
      rules: {
          title: [
            { required: true, message: '请输入标题名称', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          text: [
            { required: true, message: '请输入意见', trigger: 'blur' },
          ],
          email: [
            { required: true, message: '请输入邮箱', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' },
          ],
      }
    };
  },
  methods: {
    handleImageAdded(file, Editor, cursorLocation, resetUploader) {
       const params = new FormData();
       params.append("file",file)
       params.append("bucket",'img')
       this.axios.post('http://localhost:8083/file/upload', params).then(res=>{
         console.log(res)
        const url = "http://192.168.142.202:9000/" + res.data
        Editor.insertEmbed(cursorLocation, 'image', url)
        resetUploader()
       })
        
    },
    save() {
      this.$refs.wyform.validate(valid=>{
        if(valid){
          let params = new FormData();
      this.form.username = this.username;
      for (const key in this.form) {
        params.append(key, this.form[key]);
      }
      this.axios
        .post("http://localhost:8083/advice/save", params)
        .then((response) => {
          this.$emit("update:show", false);
          this.$message.success("提交成功");
        })
        .catch((e) => {});
        }
      })
    },
  },
};
</script>

